﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Collections.ObjectModel;

using Resxus.Model;
using Resxus.DataAccess;

using System.Windows.Forms;

using System.Windows.Input;

using Resxus.Util;

namespace Resxus.ViewModel
{
    public class FileListVM : ViewModelBase
    {

    #region Fields

        IEdit _control;         

    #endregion //Fields

    #region Constructor

        public FileListVM(IEdit control)
        {
            _control = control;

            _control.Files.AddFile += new Action<ResourceFile>(Repository_AddEvent);
            _control.Files.RemoveFile += new Action<ResourceFile>(Repository_RemoveEvent);
        }

    #endregion //Constructor

    #region Commands

        ICommand _AddCommand = null;
        public ICommand AddCommand
        {
            get
            {
                if (_AddCommand  == null)
                {
                    _AddCommand = new RelayCommand(pararm => Add());
                }
                return _AddCommand;
            }
        }
    
        ICommand _DeleteCommand = null;
        public ICommand DeleteCommand
        {
            get
            {
                if (_DeleteCommand  == null)
                {
                    _DeleteCommand = new RelayCommand(pararm => Delete(), (param) => { return (this.CheckedRowCount > 0); });
                }
                return _DeleteCommand;
            }
        }
           
    #endregion //Commands

    #region Binding Properties
        
        ObservableCollection<ResourceFile> _Files = null;
        public ObservableCollection<ResourceFile> Files
        {
            get
            {
                if (this._Files == null)
                {
                    _Files = new ObservableCollection<ResourceFile>(_control.Files);
                }
                return _Files;
            }
        }

        object _SelectedItem = 0;
        public object SelectedItem
        {
            get
            {
                return _SelectedItem;
            }
            set
            {
                _SelectedItem = value;
                OnPropertyChanged("SelectedItem");
            }
        }

        bool _IsAllChecked = false;
        public bool IsAllChecked
        {
            get
            {
                return _IsAllChecked;
            }
            set
            {
                _IsAllChecked = value;
                OnPropertyChanged("IsCheckedAll");

                this.CheckAll(this.IsAllChecked);
            }
        }
        
    #endregion //Binding Properties

    #region Private Helpers
              
        int CheckedRowCount
        {
            get 
            {
                return this.Files.Count<ResourceFile>((p) => { return p.IsChecked; });
            }
        }                

        void Add()
        {
            this._control.Load(_control.Files, "Add Resource File", (file) =>
            {
                this._control.Files.Items.Add(file);                
            });                       
        }

        void Delete()
        {
            IEnumerable<ResourceFile> items = this.Files.Where<ResourceFile>((param) => { return param.IsChecked; });
            items.DoAll<ResourceFile>((param) => 
            {
                this._control.Files.Items.Remove(param);       
            });            
        }

        void Repository_RemoveEvent(ResourceFile obj)
        {
            this.Files.Remove(obj);
        }

        void Repository_AddEvent(ResourceFile obj)
        {
            this.Files.Add(obj);
        }

    #endregion //Private Helpers

    #region Public Methods

        public void Open()
        {
            ResourceFile file = this.SelectedItem as ResourceFile;
            if (file != null)
            {
                this._control.Open(file);
            }            
        }

        public void CheckAll(bool flag)
        {
            foreach (var item in this.Files)
	        {
                item.IsChecked = flag;
	        } 
        }

    #endregion //Public Methods
      
    }
}
